import axios from "axios"
import qs from 'qs'
import { Message } from 'element-ui';

// const accessToken = sessionStorage.getItem('accessToken')
let Axios = axios.create({
	responseType: 'json',
	// timeout: 3000000,
	headers: {
		// 'token': accessToken ? accessToken : ''
		'Content-Type': 'application/json;charset=utf-8',
		// 'Accept': 'application/json'
	}
})

// 添加响应拦截器
Axios.interceptors.response.use(function (response) {
	// 对响应数据做点什么  
	if (response.status === 200) {
		if (response.data.code == "500") {
			// 操作错误
			Message.error(response.data.message);
			return Promise.reject(response.data)
		}
		if (response.data.code == "9999") {
			// 提示
			Message.error(response.data.message);
			return Promise.reject(response.data)
		}
		if (response.data.code == "403") {
			// 无访问权限
			Message.error(response.data.message);
			window.location.href = '#/home'
			return Promise.reject(response.data)
		}
		if (response.data.code == "3000") {
			// 登录失败
			Message.error(response.data.message);
			if (window.location.hash.indexOf('/login') === -1) {
				window.location.href = `#/login?formUrl=${window.location.hash}`
			}
			return Promise.reject(response.data)
		}
		if (response.data.code == "3200") {
			// token 无效
			Message.error(response.data.message);
			if (window.location.hash.indexOf('/login') === -1) {
				window.location.href = `#/login?formUrl=${window.location.hash}`
			}
			return Promise.reject(response.data)
		}
		if (response.data.code == "3100") {
			// 仪表盘没有编辑权限 有预览权限
			Message.error(response.data.message);
			window.location.href = `#/pc-index?dashboardId=${response.data.data}`
			return Promise.reject(response.data)
		}
	}
	return response.data
}, function (error) {
	/***** 接收到异常响应的处理开始 *****/
	if (error && error.response) {
		// 1.公共错误处理
		// 2.根据响应码具体处理
		switch (error.response.status) {
			case 400:
				error.message = '错误请求'
				break;
			case 401:
				error.message = '未授权，请重新登录'
				break;
			case 403:
				error.message = '拒绝访问'
				break;
			case 404:
				error.message = '请求错误,未找到该资源'
				// window.location.href = "/NotFound"
				break;
			case 405:
				error.message = '请求方法未允许'
				break;
			case 408:
				error.message = '请求超时'
				break;
			case 500:
				error.message = '服务器端出错'
				break;
			case 501:
				error.message = '网络未实现'
				break;
			case 502:
				error.message = '网络错误'
				break;
			case 503:
				error.message = '服务不可用'
				break;
			case 504:
				error.message = '网络超时'
				break;
			case 505:
				error.message = 'http版本不支持该请求'
				break;
			default:
				error.message = `连接错误${error.response.status}`
		}
		let text = ''
		if (error.response?.data?.message) {
			text = error.response.data.message
		}
		// Message.error(text || error.message)
		console.log(text || error.message);
	} else {
		// 超时处理
		if (JSON.stringify(error).includes('timeout')) {
			Message.error('服务器响应超时，请刷新当前页')
		}
		error.message = '连接服务器失败'
	}
	// 抛出错误
	// Message.error(error.message)
	return Promise.reject(error)
})



/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const get = (url, params) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.get(url, { params }, {
		headers: {
			'token': accessToken ? accessToken : ''
		}
	});
};

/**
 * post方法，对应query传参的post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const postQuery = (url, params) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, null, {
		params: params,
		headers: {
			'token': accessToken ? accessToken : ''
		}
	})
};

/**
 * post方法，对应json传参的post请求
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
 */
export const post = (url, data) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, data, {
		headers: {
			'token': accessToken ? accessToken : ''
		}
	})
};

export const postMultipart = (url, data) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, data, {
		headers: { 'Content-Type': 'multipart/form-data', 'token': accessToken ? accessToken : '' }
	})
};
/**
 * post方法，对应formdata传参的post请求
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
 */
export const postForm = (url, data, opt) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, data, {
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
			'X-Requested-With': 'XMLHttpRequest',
			'token': accessToken ? accessToken : ''
		},
		transformRequest: [function (val) {
			return qs.stringify(val)
		}],
		...opt
	});
};


/**
 * put方法，对应put请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const put = (url, params) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.put(url, params, {
		headers: {
			'token': accessToken ? accessToken : ''
		}
	});
};

/**
 * 上传方法
*/
export const upload = (url, param) => {
	const accessToken = sessionStorage.getItem('token')
	const config = {
		headers: { 'Content-Type': 'multipart/form-data', 'token': accessToken ? accessToken : '' }
	}
	return axios.post(`${baseURL}${url}`, param, config)
};


export default { get, post, postQuery, put, upload, postMultipart, Axios }